package cn.dankal.flyaudio.store;

import android.view.View;

import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.ChildrenBean;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.CommentsDetailAdapter;
import cn.dankal.flyaudio.contract.CommentsDetailContract;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;
import cn.dankal.flyaudio.presenter.CommentsDetailPresenter;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 28/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.store
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class CommentsDetailActivity extends BaseActivity implements View.OnClickListener,
        CommentsDetailContract.View {

    private CommentsDetailPresenter presenter;
    private CommentsDetailAdapter evaluateListAdapter;
    private ChildrenBean childrenBean;
    private AppCompatImageView itemIvAwesome;
    private AppCompatTextView ivMessage;
    private AppCompatEditText editContent;
    private AppCompatTextView itemTvName, itemTvContent, itemTvTime, itemTvAwesomeNum, tvAll,
            tvName;

    private String uuid;
    private String userName;
    private ChildrenBean itemChildrenBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comments_detail;
    }

    @Override
    protected void initComponents() {
        presenter = new CommentsDetailPresenter();
        presenter.attachView(this);
        childrenBean = (ChildrenBean) getIntent().getSerializableExtra("model");
        RecyclerView recyclerView = findViewById(R.id.mRv);
        evaluateListAdapter = new CommentsDetailAdapter();
        recyclerView.setAdapter(evaluateListAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        itemIvAwesome = findViewById(R.id.item_iv_awesome);
        itemTvName = findViewById(R.id.item_tv_name);
        itemTvContent = findViewById(R.id.item_tv_content);
        itemTvTime = findViewById(R.id.item_tv_time);
        itemTvAwesomeNum = findViewById(R.id.item_tv_awesome_num);
        tvAll = findViewById(R.id.tv_all);
        ivMessage = findViewById(R.id.iv_message);
        editContent = findViewById(R.id.edit_content);
        tvName = findViewById(R.id.tv_name);
        itemTvName.setText(childrenBean.getCommentUser().getUsername());
        itemTvTime.setText(childrenBean.getCreatedDatetime());
        itemTvAwesomeNum.setText(childrenBean.getLikeCount());
        itemTvContent.setText(childrenBean.getContent());

        itemIvAwesome.setOnClickListener(this);
        setLike();

        int size = 0;
        if (childrenBean.getChildren() != null) {
            size = childrenBean.getChildren().size();
        }
        tvAll.setText("全部回复（" + size + "）");
        evaluateListAdapter.addDatas(childrenBean.getChildren(), false);
        ivMessage.setOnClickListener(this);
        uuid = childrenBean.getUuid();
        userName = childrenBean.getCommentUser().getUsername();
        tvName.setText("回复 " + userName + "：");

        LiveEventBus.get("ChildrenBean", ChildrenBean.class).observe(this,
                new Observer<ChildrenBean>() {
                    @Override
                    public void onChanged(ChildrenBean childrenBean) {
                        uuid = childrenBean.getUuid();
                        userName = childrenBean.getCommentUser().getUsername();
                        tvName.setText("回复 " + userName + "：");
                    }
                });
        LiveEventBus.get("Like", ChildrenBean.class).observe(this, new Observer<ChildrenBean>() {
            @Override
            public void onChanged(ChildrenBean childrenBean) {
                itemChildrenBean = childrenBean;
                if ("true".equals(childrenBean.getiLike())) {
                    presenter.subUnLike(childrenBean.getUuid());
                } else {
                    presenter.subLike(childrenBean.getUuid());
                }
            }
        });

    }

    @Override
    public void evaluateSuccess(CommentsEntity commentsEntity) {
//        this.childrenBean = commentsEntity;

    }

    @Override
    public void likeSuccess() {
        childrenBean.setiLike("true");
        setLike();

    }

    @Override
    public void unLikeSuccess() {
        childrenBean.setiLike("false");
        setLike();
    }

    @Override
    public void subLikeSuccess() {
        itemChildrenBean.setiLike("true");
        int count = 0;
        if (!StringUtil.isEmpty(itemChildrenBean.getLikeCount())) {
            count = Integer.valueOf(itemChildrenBean.getLikeCount());
        }
        itemChildrenBean.setLikeCount((count++) + "");
        evaluateListAdapter.notifyDataSetChanged();
    }

    @Override
    public void subUnLikeSuccess() {
        itemChildrenBean.setiLike("false");
        int count = 0;
        if (!StringUtil.isEmpty(itemChildrenBean.getLikeCount())) {
            count = Integer.valueOf(itemChildrenBean.getLikeCount());
        }
        itemChildrenBean.setLikeCount((count--) + "");
        evaluateListAdapter.notifyDataSetChanged();

    }

    private void setLike() {
        if ("true".equals(childrenBean.getiLike())) {
            itemIvAwesome.setImageResource(R.mipmap.ic_awesome);
        } else {
            itemIvAwesome.setImageResource(R.mipmap.ic_awesome_un);
        }
    }

    @Override
    public void sendSuccess() {
        LiveEventBus.get("EvaluateRefresh", String.class).post("1");
        finishActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_message:
                if (StringUtil.isEmpty(editContent.getText().toString())) {
                    ToastUtils.showShort("请输入评论内容");
                    return;
                }
                presenter.sendMessage(uuid, editContent.getText().toString());
                break;
            case R.id.item_iv_awesome:
                if ("true".equals(childrenBean.getiLike())) {
                    presenter.unLike(childrenBean.getUuid());
                } else {
                    presenter.like(childrenBean.getUuid());
                }
                break;
        }
    }
}
